package com.terrynoya.fight.test
{
	import com.terrynoya.fight.animations.Clsn;
	import com.terrynoya.fight.combat.Collisions;
	import com.terrynoya.fight.emuns.ClsnType;
	import com.terrynoya.fight.emuns.Facing;
	import com.terrynoya.geom.MVector2D;
	
	import flash.geom.Rectangle;

	public class CollisionEntity
	{
		private var _localCollision:Array;
		
		private var _globalCollison:Array;
		
		public function CollisionEntity(localClsn:Array = null)
		{
			this._localCollision = localClsn;
		}

		public function get globalCollison():Array
		{
			return _globalCollison;
		}

		public function set globalCollison(value:Array):void
		{
			_globalCollison = value;
		}

		public function get localCollision():Array
		{
			return _localCollision;
		}

		public function set localCollision(value:Array):void
		{
			_localCollision = value;
		}
		
		public function localToGlobalCol(pos:MVector2D,fightStageHeight:int,facing:int):Array
		{
			var rlt:Array = new Array();
			var charaPosAtStage:MVector2D = pos.add(new MVector2D(0,fightStageHeight));
			if(this._localCollision == null)
			{
				return null;
			}
			for (var i:int = 0; i < this._localCollision.length; i++) 
			{
				var clsn:Clsn = this._localCollision[i];
				var rect:Rectangle = clsn.rect;
				var rectX:Number = rect.x;
				if(facing == Facing.LEFT)
				{
					rectX = (rect.width + rect.x) * -1;
				}
				var dx:Number = rectX + charaPosAtStage.x;
				var dy:Number = rect.y + charaPosAtStage.y;
				var globalRect:Rectangle = new Rectangle(dx,dy,rect.width,rect.height);
				var globalClsn:Clsn = new Clsn(clsn.type);
				globalClsn.rect = globalRect;
				rlt.push(globalClsn);
			}
			this._globalCollison = rlt;
			return rlt;
		}
		
		public function getGlobalRight():Number
		{
			var rects:Array = this._globalCollison;
			if(rects.length == 0)
			{
				return 0;
			}
			else if(rects.length == 1)
			{
				return Clsn(rects[0]).right;
			}
			var max:Clsn = rects[0]; 
			for (var i:int = 1; i < rects.length; i++) 
			{
				var rect:Clsn = rects[i];
				if(rect.right > max.right)
				{
					max = rect;
				}
			}
			return max.right;
		}
		
		public function getGlobalLeft():Number
		{
			var rects:Array = this._globalCollison;
			if(rects.length == 0)
			{
				return 0;
			}
			else if(rects.length == 1)
			{
				return Clsn(rects[0]).left;
			}
			var min:Clsn = rects[0]; 
			for (var i:int = 1; i < rects.length; i++) 
			{
				var rect:Clsn = rects[i];
				if(rect.left < min.left)
				{
					min = rect;
				}
			}
			return min.left;
		}
		
		public function hitTestNormal(value:CollisionEntity):Boolean
		{
			return CollisionEntity.collision(this._globalCollison,ClsnType.Type2Normal,value.globalCollison,ClsnType.Type2Normal);
		}
		
		public function hitTestAttack(value:CollisionEntity):Boolean
		{
			return CollisionEntity.collision(this._globalCollison,ClsnType.Type1Attack,value.globalCollison,ClsnType.Type2Normal);
		}
		
		/**
		 * 2组多个矩形进行碰撞检测
		 * 一组中的任何一个和另一组的任何一个有碰撞返回true
		 * 一组中的所有矩形和另一个组的所有矩形都未碰撞，返回false
		 * @param rects1
		 * @param rects2
		 * @return 
		 * 
		 */		
		public static function collision(clsnArr1:Array,clsn1Type:int,clsnArr2:Array,clsn2Type:int):Boolean
		{
			var rlt:Boolean = false;
			if(clsnArr1 == null || clsnArr2 == null)
			{
				return false;
			}
			for (var i:int = 0; i < clsnArr1.length; i++) 
			{
				var isCollide:Boolean = false;
				var clsn1:Clsn = clsnArr1[i];
				if(clsn1.type != clsn1Type)
				{
					continue;
				}
				var rect1:Rectangle = clsn1.rect;
				for (var j:int = 0; j < clsnArr2.length; j++) 
				{
					var clsn2:Clsn = clsnArr2[j];
					if(clsn2.type != clsn2Type)
					{
						continue;
					}
					var rect2:Rectangle = clsn2.rect; 
					if(rect1.intersects(rect2))
					{
						isCollide = true;	
						break;
					}
				}
				if(isCollide)
				{
					rlt  = true;
					break;
				}
			}
			return rlt;
		}
	}
}